﻿using SmartSapce.ServiceLocation;
using SmartSpace.Core;
using SmartSpace.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SmartSpace.WebApp._console.page
{
    public partial class page_design : Page
    {
        private Guid _pageId { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreInit(EventArgs e)
        {
            _pageId = new Guid(Request.QueryString["id"]);

            _ctlWebPartManager.Personalization.InitialScope = PersonalizationScope.Shared;

            ICoreService service = ServiceLocator.Get<ICoreService>();
            VirtualPage page = service.GetPage(SsSite.Application.Id, _pageId);

            _ctlPanel.Controls.Add(this.LoadControl("~/_templates/_page/" + page.Template.Name + ".ascx"));

            (CatalogZone1.Controls[0] as DeclarativeCatalogPart).WebPartsTemplate = new CategoryTemplate(this, _ctlWebPartManager);


            base.OnPreInit(e);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }

        protected override void FrameworkInitialize()
        {
            base.FrameworkInitialize();
        }

        protected void _ctlBtnEdit_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.EditDisplayMode;
        }

        protected void _ctlBtnCategory_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.CatalogDisplayMode;
        }

        protected void _ctlBtnDesign_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.DesignDisplayMode;
        }

        protected void _ctlBtnBroswer_Click(object sender, EventArgs e)
        {
            _ctlWebPartManager.DisplayMode = WebPartManager.BrowseDisplayMode;
        }
    }

    public class CategoryTemplate : ITemplate
    {
        public Page _page;
        private WebPartManager _webPartManager;

        public CategoryTemplate(Page page, WebPartManager webPartManager)
        {
            _page = page;
            _webPartManager = webPartManager;
        }

        public void InstantiateIn(Control container)
        {
            ICoreService service = ServiceLocator.Get<ICoreService>();
            PagedList<Module> modules = service.GetModules("_webpart", 0, 999);

            if (modules.Count == 0)
            {
                LoadTestParts1(container);
                LoadTestParts2(container);
            }
            else
            {
                foreach (var module in modules)
                {
                    foreach (var view in module.Views)
                    {
                        Control control = _page.LoadControl(view.Name);
                        control.ID = view.Id.ToString();

                        WebPart part = _webPartManager.CreateWebPart(control);
                        part.Title = view.Caption;
                        part.Description = view.Caption;
                        container.Controls.Add(part);
                    }
                }
            }
        }

        private void LoadTestParts1(Control container)
        {
            Control control = _page.LoadControl("~/_modules/_system/accountdetail.ascx");
            control.ID = "ctl_accountDetail";// +Guid.NewGuid().ToString();

            WebPart part = _webPartManager.CreateWebPart(control);
            part.ID = "part_accountDetail";
            //part.ChromeType = PartChromeType.None;
            
            part.Title = "accountdetail";
            part.Description = "accountdetail";
            container.Controls.Add(part);
        }

        private void LoadTestParts2(Control container)
        {
            Control control = _page.LoadControl("~/_modules/_system/accountlog.ascx");
            control.ID = "ctl_accountLog";// +Guid.NewGuid().ToString();

            WebPart part = _webPartManager.CreateWebPart(control);
            part.ID = "part_accountLog";
            //part.ChromeType = PartChromeType.None;

            part.Title = "accountlog";
            part.Description = "accountlog";
            container.Controls.Add(part);
        }
    }
}